<?php
namespace BP\Pub;
require_once("BP/DA/DBAccess.php");
require_once("BP/DA/DataRow.php");
require_once("BP/DA/DataTable.php");
require_once("BP/Sys/PubClass.php");
require_once("BP/Sys/SystemConfig.php");
require_once("BP/Tools/ConvertTools.php");
class RepBill extends DataIOEn {
	public static function constructor__ () 
	{
		$me = new self();
		$me->Title = "WFV3.0单据自动修复线�??";
		return $me;
	}
	public function Do () 
	{
		$msg = "";
		$sql = "  SELECT * FROM WF_BillTemplate";
		$dt = DBAccess->RunSQLReturnTable($sql);
		foreach ($dt->Rows as $dr) 		{
			$file = (($SystemConfig->getPathOfCyclostyleFile() + $dr->getValue("URL")) . ".rtf");
			$msg += RepBill::RepairBill($file);
		}
	}
	public static function RepairBill ($file) // [String file]
	{
		$msg = "";
		$docs = "";
		try 
		{
			$docs = $ConvertTools->StreamReaderToStringConvert($file, "ASCII");
		}
		catch (Exception $ex)
		{
			$ex->printStackTrace();
			return ((("@读取单据模板时出现错误�?�cfile=" . $file) . " @Ex=") . $ex->getMessage());
		}
		$docs = RepBill::RepairLineV2($docs);
		try 
		{
			$ConvertTools->StreamWriteConvert($docs, $file);
		}
		catch (Exception $ex)
		{
			$ex->printStackTrace();
			return ((("@写入单据模板时出现错误�?�cfile=" . $file) . " @Ex=") . $ex->getMessage());
		}
		$msg += (("@单据:[" . $file) . "]成功修复�?");
		return $msg;
	}
	public static function RepairLine ($line) // [String line]
	{
		$chs = $line->toCharArray();
		$str = "";
		foreach ($chs as $ch) 		{
			if (($ch . '\\'))
			{
				$line = $line->replace(("\\" . $str), "");
				$str = "";
			}
			else
				if (($ch . ' '))
				{
					$line = $line->replace((("\\" . $str) . $ch), "");
					$str = "sssssssssssssssssss";
				}
				else
				{
					$str += (new Character($ch));
				}
		}
		$line = $line->replace("{", "");
		$line = $line->replace("}", "");
		$line = $line->replace("\r", "");
		$line = $line->replace("\n", "");
		$line = $line->replace(" ", "");
		$line = $line->replace("..", ".");
		return $line;
	}
	public static function RepairLineV2 ($docs) // [String docs]
	{
		$chars = $docs->toCharArray();
		$strs = "";
		foreach ($chars as $c) 		{
			if (($c . '<'))
			{
				$strs = "<";
				continue;
			}
			if (($c . '>'))
			{
				$strs += (new Character($c));
				$line = $strs;
				$line = RepBill::RepairLine($line);
				$docs = $docs->replace($strs, $line);
				$strs = "";
				continue;
			}
			$strs += (new Character($c));
		}
		return $docs;
	}
}
?>
